package com.example.snowli.fragments;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.UserDictionary;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.example.snowli.MainActivity;
import com.example.snowli.R;

import java.util.List;

public class InsertFragment extends Fragment {

    private EditText word;
    private EditText tran;
    private Button btnInsert;




    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View insertPage = inflater.inflate(R.layout.activity_insert_fragment,null);

        //绑定对应控件
        word = insertPage.findViewById(R.id.word);
        tran = insertPage.findViewById(R.id.tran);
        btnInsert = insertPage.findViewById(R.id.btn_insert);

        //保存内容
        saveWords();



        return insertPage;

    }

    private void saveWords() {

        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入框的值
                String insertWord = word.getText().toString();
                String translation = tran.getText().toString();
                if (insertWord.equals("")){
                    //弹窗提示请输入单词
                    new AlertDialog.Builder(getContext())
                            .setTitle("请输入单词")
                            .setMessage("")
                            .show();
                    return;
                }
                if (translation.equals("")){
                    //弹窗提示请输入翻译
                    new AlertDialog.Builder(getContext())
                            .setTitle("请输入翻译")
                            .setMessage("")
                            .show();
                    return;
                }

                ContentValues cValue = new ContentValues();
                cValue.put("word",insertWord);
                cValue.put("tran",translation);
                //存入数据库
                MainActivity.db.insert("words",null,cValue);
                //弹窗 插入成功
                //弹窗提示 购买成功
                new AlertDialog.Builder(getContext())
                        .setTitle("单词录入成功")
                        .setMessage("")
                        .show();
                word.setText("");
                tran.setText("");

            }
        });



    }





}

